'use strict';
angular.module('app.controllers')
    .controller('AdminHrReportCtrl', function($scope, $window, $location, HotelService) {
        var vm = this;
        vm.year = $window.sessionStorage.year;
        vm.month = $window.sessionStorage.month;
        $scope.role = $window.sessionStorage.subrole;
        vm.host = $location.host();
        $scope.years = [2015, 2016,2017,2018,2019,2020];
        $scope.months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
        vm.region = { id: 0 };
        vm.cluster = { id: 0 };
        if (!$scope.regions) {
            $scope.regions = [];
            HotelService.getRegions().then(function(ret) {
                if ($window.sessionStorage.subrole == 'Sub-Region Admin') {
                    angular.forEach(ret.data.value, function(value, key) {
                        if ($window.sessionStorage.region == value.name) {
                            $scope.regions.push(value);
                            vm.region.id = value.id;
                            vm.regionChange();
                        }
                    })
                } else {
                    $scope.regions = ret.data.value;

                }
            })
        }

        vm.reportlink = '';
        vm.regionChange = function() {
            if (vm.region == null) {
                vm.region = { id: 0 };
                vm.cluster = { id: 0 };
            } else {
                HotelService.getClusters(vm.region.id).then(function(ret) {
                    $scope.clusters = ret.data.value;

                })
            }


            if (vm.cluster == null) {
                vm.cluster = { id: 0 };
            }

            vm.reportlink = '/api/hr/sum/export/' + vm.year + '/' + vm.month + '/' + vm.region.id + '/' + vm.cluster.id;
            vm.gcreportlink = '/api/gcpp/sum/export/' + vm.year;
            vm.directoryReportLink = '/api/dir/export';
            vm.submissionReportLink = '/api/sub/export/' + vm.year + '/' + vm.month;
            vm.leaderReportLink = '/api/leader/export/' + vm.year + '/' + vm.month;
            vm.staffBReportLink = '/api/staffingb/export/' + vm.year + '/' + vm.month;
        }
        vm.reportlink = '/api/hr/sum/export/' + vm.year + '/' + vm.month + '/' + vm.region.id + '/' + vm.cluster.id;
        vm.gcreportlink = '/api/gcpp/sum/export/' + vm.year;
        vm.directoryReportLink = '/api/dir/export';
        vm.submissionReportLink = '/api/sub/export/' + vm.year + '/' + vm.month;
        vm.leaderReportLink = '/api/leader/export/' + vm.year + '/' + vm.month;
        vm.staffBReportLink = '/api/staffingb/export/' + vm.year + '/' + vm.month;

    })
    .controller('HotelReportCtrl', function($scope, $window, HrService, ngDialog, $location) {
        var vm = this;
        vm.year = $window.sessionStorage.year;
        vm.month = $window.sessionStorage.month;
        vm.host = $location.host();
        vm.hotelId = $window.sessionStorage.hotelId;
        vm.reportlink = '';
        vm.reportlink2 = '';

        vm.reportlink = '/api/hr/export/monthly/' + vm.year + '/' + vm.hotelId;
        vm.reportlink2 = '/api/training/export/monthly/' + vm.year + '/' + vm.hotelId;


        vm.loadData = function() {
            HrService.turnoverInterviewSummaryType({
                year: vm.year,
                month: vm.month,
                hotelId: vm.hotelId
            }).then(function(ret) {
                vm.sumType = ret.data.value;
                var typeload = {
                    name: 'Exit Interview',
                    datapoints: []
                };
                angular.forEach(vm.sumType, function(value, key) {
                    if (value.type == 'job') {
                        typeload.datapoints.push({
                            x: 'Job',
                            y: value.value
                        });
                    } else if (value.type == 'personal') {
                        typeload.datapoints.push({
                            x: 'Personal',
                            y: value.value
                        });
                    } else if (value.type == 'other') {
                        typeload.datapoints.push({
                            x: 'Others',
                            y: value.value
                        });
                    }
                });
                $scope.inteviewdata1 = [typeload];
            });

            HrService.turnoverInterviewSummaryTop({
                year: vm.year,
                month: vm.month,
                hotelId: vm.hotelId
            }).then(function(ret) {
                if (ret.data.value.length > 0) {
                    vm.sumTop = ret.data.value;
                    var pageload = {
                        name: 'YTD Top 5 resignation reasons',
                        datapoints: []
                    };
                    angular.forEach(vm.sumTop, function(value, key) {
                        pageload.datapoints.push({
                            x: value.type,
                            y: value.value
                        });
                    })
                    $scope.inteviewdata2 = [pageload];
                }
            })
            HrService.recruitReportGet(vm.year, vm.month, 0, 0, $window.sessionStorage.hotelId).then(function(ret) {
                vm.sourceApp = ret.data.value.sourceApp;
                vm.sourceNew = ret.data.value.sourceNew;
                vm.convention = ret.data.value.conventionNew;
                var data1 = {
                    name: 'Source of Applicants',
                    datapoints: []
                };
                angular.forEach(vm.sourceApp, function(value, key) {
                    if (value.type != 'No. of employess recruited are the ones who return to their hometown') {
                        data1.datapoints.push({
                            x: value.type,
                            y: value.value
                        });
                    }
                })
                $scope.data1 = [data1];
                var data2 = {
                    name: 'Source of New Comers',
                    datapoints: []
                };
                angular.forEach(vm.sourceNew, function(value, key) {
                    if (value.type != 'No. of employess recruited are the ones who return to their hometown') {
                        data2.datapoints.push({
                            x: value.type,
                            y: value.value
                        });
                    }
                })
                $scope.data2 = [data2];
                var data3 = {
                    name: 'Source of New Comers Convention Rate',
                    datapoints: []
                };
                angular.forEach(vm.convention, function(value, key) {
                    if (value.type != 'No. of employess recruited are the ones who return to their hometown') {
                        data3.datapoints.push({
                            x: value.type,
                            y: value.value / 10000
                        });
                    }
                })
                $scope.data3 = [data3];

                $scope.data4 = [data2, data1];

            });

            HrService.pathwayGet(vm.year, 1, $window.sessionStorage.hotelId).then(function(ret) {
                vm.pathway = ret.data.value;
                var pdata1 = {
                    name: 'Career Pathways communication',
                    datapoints: []
                };
                var pdata2 = {
                    name: 'E-learning',
                    datapoints: []
                };
                var pdata3 = {
                    name: 'PDP conversation',
                    datapoints: []
                };
                var pdata4 = {
                    name: 'Promotion',
                    datapoints: []
                };
                var pdata5 = {
                    name: 'Internal move(within IHG)',
                    datapoints: []
                };
                angular.forEach(vm.pathway, function(value, key) {

                })
            })
        }

        vm.loadData();

        $scope.config = {
            title: '',
            subtitle: '',
            debug: false,
            showXAxis: true,
            showYAxis: true,
            showLegend: true,
            calculable: true,
            //theme: 'shine',
            stack: false,
            toolbox: {
                show: true,
                feature: {
                    //mark : {show: true},
                    dataView: {
                        show: true,
                        readOnly: false
                    },
                    restore: {
                        show: true
                    },
                    saveAsImage: {
                        show: true
                    }
                }
            },
        };
        $scope.configBar = {
            title: '',
            subtitle: '',
            debug: false,
            showXAxis: true,
            showYAxis: true,
            showLegend: true,
            calculable: true,
            //theme: 'shine',
            stack: true,

            // dataZoom: {
            //     show: true,
            //     start : 0
            // },
            xAxis: {
                axisLabel: {
                    interval: 0,
                    rotate: -10,
                    margin: 0,
                    // formatter:function(val){
                    //     return val.split(" ").join("\n");
                    // },
                    textStyle: {
                        align: 'left',
                        baseline: 'top',
                        fontSize: 7
                    }
                }
            },
            toolbox: {
                show: true,
                feature: {
                    //mark : {show: true},
                    dataView: {
                        show: true,
                        readOnly: false
                    },
                    restore: {
                        show: true
                    },
                    saveAsImage: {
                        show: true
                    }
                }
            },
        };
    })
